package com.gitee.feizns.itrip.personal.beans.vo.impl;

import com.gitee.feizns.itrip.personal.beans.vo.Page;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author feizns
 * @since 2019/1/5 0005
 */
public class BiPageVoForSpringData<T, R> implements Page<R> {

    private org.springframework.data.domain.Page<T> page;

    private Function<T, R> convert;

    public BiPageVoForSpringData(org.springframework.data.domain.Page<T> page, Function<T, R> convert) {
        this.convert = convert;
        this.page = page;
    }

    @Override
    public Integer getBeginPos() {
        return page.getNumber() * page.getSize();
    }

    @Override
    public Integer getCurPage() {
        return page.getNumber() + 1;
    }

    @Override
    public Integer getPageCount() {
        return page.getTotalPages();
    }

    @Override
    public Integer getPageSize() {
        return page.getSize();
    }

    @Override
    public List<R> getRows() {
        if ( page.getContent() != null )
            return page.getContent().stream().map(convert).collect(Collectors.toList());
        return null;
    }

    @Override
    public Long getTotal() {
        return page.getTotalElements();
    }

}
